﻿#include "jsontest.h"
#include <QDebug>

namespace jsontest{
JsonTest::JsonTest()
{

}

std::string JsonTest::SerializeAsString()
{
    //    Person p;
    //    p.name = "hxj";
    //    p.age = 10;
    //    p.numbs.emplace_back(1);
    //    p.numbs.emplace_back(2);
    //    p.numbs.emplace_back(3);
    //    p.numbs.emplace_back(4);

    Person2 pe;
    pe.name = "hxj";
    pe.age = 10;
    Subject yuw;
    yuw.subject = "yuwen";
    yuw.score = 15;
    pe.numbs.push_back(yuw);
    yuw.subject = "su";
    pe.numbs.push_back(yuw);


    GrpcEventJsonAvis p;
    //p.SourceId = QString(u8"啊范德萨发").toUtf8().toStdString();
    p.SourceId = u8R"(啊范德萨发)";
    p.TargetId = "safas7";
    p.EventName = "hhhhh";

    GrpcEventJsonAvisSub a;
    a.age = 10;
    a.name = "asfasf";

    JsonArray arr;
    arr.Add(a);
    arr.Add(pe);
    arr.Add(345);
    arr.Add(std::string("sdgfgd"));
    arr.Add(std::string("11111"));
    arr.Add(1);

    //p.param = arr.toJson();
    //p.param = pe;


    std::list<std::string> ttttt;
    ttttt.push_back("afsaf ");
    ttttt.push_back("asfas 23");

    std::list<Subject> ssss;
    ssss.push_back(yuw);
    ssss.push_back(yuw);
    p.param = CreateJsonArray(ssss,345, 1.57, std::string("sdgfgd"), std::string("11111"), 1);
    //p.param = CreateJsonArray(ttttt, 345, 1.57, std::string("sdgfgd"), std::string("11111"), 1);



    nlohmann::json j2 = p;

    std::string str = ::SerializeAsString(p);

    qDebug() << str.c_str();
    return str;
}

void JsonTest::ParseFromString(const std::string &json)
{
    std::pair<GrpcEventJsonAvis, bool> ret = ::ParseFromString<GrpcEventJsonAvis>(json);
    if(ret.second)
    {

        //try catch
        qDebug() << ret.first.SourceId.c_str() ;

        std::list<Subject> a = ret.first.param[0].get<std::list<Subject>>();

        //std::list<std::string> ttt = ret.first.param[0].get<std::list<std::string>>();

        qDebug() << ret.first.param[1].get<double>();
        qDebug() << ret.first.param[2].get<double>();
        qDebug() << ret.first.param[3].get<std::string>().c_str();



        //qDebug() << ret.first.param.get<GrpcEventJsonAvisSub>() ;
    }

}

}

